<?php
	require '../biz/backend.php';

	class action extends backend
	{
		var $get_privilege_id = 86;
		var $post_privilege_id = 86;

		function do_get()
		{
			$argument = load( 'biz.argument' );
			$module_type = $argument->content_module_cname;
			$id = isset( $this->input['id'] ) ? $this->input['id'] : '';

			if( $id != '' )
			{
				$cate = load( 'biz.cate' );
				$data = $cate->get( array( 'id'=>$id ) );
				$template = load( 'template', array( 'domain'=>'tpl.admin' ) );

				$template->assign( 'reference', isset( $this->input['reference'] ) ? urldecode( $this->input['reference'] ) : config( 'server.HTTP_REFERER' ) );
				$template->assign( 'current_url', config( 'current_url' ) );
				$template->assign( 'module_type', $module_type );
				$template->assign( 'data', $data );
				echo $template->parse( 'set_column.php' );
			}
		}

		function do_post()
		{
			$url = isset( $this->input['reference'] ) ? $this->input['reference'] : config( 'domain.admin' ) . 'list_column.php';
			unset( $this->input['reference'] );
			unset( $this->input['superset'] );

			$cate = load( 'biz.cate' );

			//父栏目不能是本身
			if ( $this->input['id'] == $this->input['parent_id'] )
			{
				$this->prompt( '父栏目不能是自身', array( array( 'url' => config( 'server.HTTP_REFERER' ), 'name' => '返回' ), array( 'url' => 'list_column.php', 'name' => '返回栏目列表' ) ) );
				exit;
			}

			//判断是否是移动到自己的子栏目下
			$ischild = $cate->isChild( $this->input['id'], $this->input['parent_id'] );
			if ( $ischild )
			{
				$this->prompt( '父栏目不能是自身的子栏目', array( array( 'url' => config( 'server.HTTP_REFERER' ), 'name' => '返回' ), array( 'url' => 'list_column.php', 'name' => '返回栏目列表' ) ) );
				exit;
			}

			if( preg_match( '!^\w+$!', $this->input['ename'] ) )
			{
				//判断栏目是否已存在
				$cateinfo = $cate->get( $this->input );
				if ( $cateinfo['ename'] != $this->input['ename'] || $cateinfo['parent_id'] != $this->input['parent_id'] )
				{
					$database = load( 'database' );
					$sql = "select id from cate where ename='" . $this->input['ename'] . "' and parent_id=" . $this->input['parent_id'];
					$data = $database->select( $sql );
					if( isset( $data[0] ) )
					{
						$this->prompt( '该栏目已存在', array( array( 'url' => config( 'server.HTTP_REFERER' ), 'name' => '返回' ), array( 'url' => 'list_column.php', 'name' => '返回栏目列表' ) ) );
						exit;
					}
				}
			}
			else
			{
				$this->prompt( '栏目英文名称只能为英文字母', array( array( 'url' => config( 'server.HTTP_REFERER' ), 'name' => '返回' ), array( 'url' => 'list_column.php', 'name' => '返回栏目列表' ) ) );
				exit;
			}

			// 非顶级栏目不能设置二级域名
			if( $this->input['parent_id'] !=0 && ( $this->input['dev_domain'] != '' || $this->input['product_domain'] != '' ) )
			{
				$this->input['dev_domain'] = '';
				$this->input['product_domain'] = '';
			}

			//修改栏目信息
			$result = $cate->set( $this->input );

			if ( $result['success'] )
			{
				$this->prompt( '成功修改栏目', array( array( 'url' => config( 'domain.admin' ) . 'set_column.php?id=' . $this->input['id'] . '&reference=' . urlencode( $url ), 'name' => '继续修改栏目' ), array( 'url' => 'list_column.php', 'name' => '返回栏目列表' ) ) );
			}
			else
			{
				$this->prompt( '添加栏目失败', $this->transform( $result['message'], config( 'server.HTTP_REFERER' ) ) );
			}
		}

	}

	$action = new action();
	$action->run();
?>